package org.jsola.hr.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jsola.admin.constant.AdminConstants;
import org.jsola.contract.dto.TemplateUpdateDTO;
import org.jsola.exception.ParamException;
import org.jsola.hr.dto.HrCategoryUpdateDTO;
import org.jsola.hr.dto.TemplateVariableUpdateDTO;
import org.jsola.hr.provider.IContractProviderService;
import org.jsola.hr.service.IHrContractService;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 合同模块接口
 *
 * @author zhenghua
 */
@Service("hrHrContractServiceServiceImpl")
@Slf4j
public class HrContractServiceServiceImpl implements IHrContractService {

    @Autowired
    private IContractProviderService contractProviderService;

    @Override
    @Transactional(value = AdminConstants.TX, rollbackFor = Exception.class)
    public void updateCategoryByCompany(HrCategoryUpdateDTO hrCategoryUpdateDTO, TokenUser tokenUser) {
        // 修改合同类型
        int updateContract = contractProviderService.updateCategory(hrCategoryUpdateDTO, tokenUser);
        if (updateContract == 0) {
            log.error("修改同类型参数：{}，Token：{}", updateContract, tokenUser);
            throw new ParamException("更新合同类型失败，请刷新页面后重试");
        }
        TemplateUpdateDTO templateUpdateDTO = hrCategoryUpdateDTO.getTemplateUpdateDTO();
        if (templateUpdateDTO != null) {
            // 修改合同模板
            int updateTemplate = contractProviderService.updateTemplate(templateUpdateDTO, tokenUser);
            if (updateTemplate == 0) {
                log.error("修改合同模板参数：{}，Token：{}", templateUpdateDTO, tokenUser);
                throw new ParamException("更新合同模板失败，请刷新页面后重试");
            }
        }
        TemplateVariableUpdateDTO templateVariableUpdateDTO = hrCategoryUpdateDTO.getTemplateVariableUpdateDTO();
        // 修改模板关联变量
        if (templateVariableUpdateDTO != null) {
            // 修改合同模板
            contractProviderService.updateTemplateVariable(templateVariableUpdateDTO, tokenUser);
        }
    }
}
